package moltencore.network.protocol;

import spark.network.Util;
import spark.network.protocol.Base;

public class PlayerScoreUpdate extends Base{

	public static final short TYPE = 0x61;
	private static final byte[] TYPE_BYTES = Util.makeBytes(TYPE);

	private long fieldId;
	private static final int FIELD_ID_OFFSET = Base.SIZE;
	private static final int FIELD_ID_LENGTH = Long.SIZE / 8;

	private int newScore;
	private static final int NEW_SCORE_OFFSET = FIELD_ID_OFFSET + FIELD_ID_LENGTH;
	private static final int NEW_SCORE_LENGTH = Integer.SIZE / 8;

	private static final int SIZE =
		Base.SIZE +
		FIELD_ID_LENGTH +
        NEW_SCORE_LENGTH;

	public PlayerScoreUpdate(){
		super(true, TYPE_BYTES);
	}

	public PlayerScoreUpdate(long fieldId, int score){
		super(true, TYPE_BYTES);
		this.fieldId = fieldId;
		this.newScore = score;
	}

	@Override
	public void getBytes(byte[] buffer) {
		super.getBytes(buffer);
		Util.arraycopy(Util.makeBytes(fieldId), 0, buffer, FIELD_ID_OFFSET);
		Util.arraycopy(Util.makeBytes(newScore), 0, buffer, NEW_SCORE_OFFSET);
	}

	public long getFieldId() {
		return fieldId;
	}

	public void setFieldId(long fieldId) {
		this.fieldId = fieldId;
	}

	public int getNewScore(){
		return newScore;
	}

	public void setNewScore(int newScore){
		this.newScore = newScore;
	}

	@Override
	public void fill(byte[] buffer) {
		super.fill(buffer);
		fieldId = Util.makeLong(buffer, FIELD_ID_OFFSET);
		newScore = Util.makeInt(buffer, NEW_SCORE_OFFSET);
	}

	@Override
	public int getSize() {
		return SIZE;
	}

	public static PlayerScoreUpdate create(byte[] buffer, long receiveTime) {
		PlayerScoreUpdate packet = new PlayerScoreUpdate();
		packet.fill(buffer);
		return packet;
	}
}
